package com.youle.service;

import com.youle.enums.ExceptionEnum;
import com.youle.exception.YLException;
import com.youle.mapper.ItemCategoryMapper;
import com.youle.item.pojo.ItemCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by
 * 查询商品分类的service
 * @author touni
 * @date 2019/4/17
 * @time 18:59
 */
@Service
public class ItemCategoryService {



    @Autowired
    private ItemCategoryMapper itemCategoryMapper;



    public ResponseEntity<List<ItemCategory>> listItemCategoryByParentId(Long pid) {
        ItemCategory itemCategory = new ItemCategory();
        itemCategory.setParentId(pid);
        List<ItemCategory> itemCategoryList = itemCategoryMapper.select(itemCategory);
        if (CollectionUtils.isEmpty(itemCategoryList)){
            throw new YLException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return ResponseEntity.ok(itemCategoryList);
    }

    @Transactional
    public ResponseEntity<ItemCategory> addItemCategoryByParenId(String name, Long parentId, Integer sort) {
        ItemCategory itemCategory = new ItemCategory();
        itemCategory.setParentId(parentId);
        itemCategory.setIsParent(false);
        itemCategory.setName(name);
        itemCategory.setSort(sort);
        int flag = itemCategoryMapper.insert(itemCategory);
        if (1 !=flag){
            throw new YLException(ExceptionEnum.INSERT_CATEGORY_FAIL);
        }
        //需要将父节点的isparent属性置为true   todo 此处前端没有进行回显，导致在新创建的类目下在不刷新的情形下再次添加会抛出空指针
        ItemCategory itemCategoryParent = new ItemCategory();
        itemCategoryParent.setId(parentId);
        itemCategoryParent = itemCategoryMapper.selectByPrimaryKey(itemCategoryParent);
        if (!itemCategoryParent.getIsParent()) {
            itemCategoryParent.setIsParent(true);
            int flagParent = itemCategoryMapper.updateByPrimaryKeySelective(itemCategoryParent);
            if (1 != flagParent){
                throw new YLException(ExceptionEnum.INSERT_CATEGORY_FAIL);
            }
        }

        return ResponseEntity.ok(itemCategory);
    }

    public ResponseEntity deleteItemCategoryById(Long id) {
        ItemCategory itemCategory = new ItemCategory();
        itemCategory.setId(id);
        //删除需要判断删除的节点是否有子节点和判断父节点是否有其他的子节点
        itemCategory = itemCategoryMapper.selectByPrimaryKey(itemCategory);
        if (itemCategory.getIsParent()){
            //如果是父节点，说明下面有子节点，则不允许删除
            throw new YLException(ExceptionEnum.DELETE_CATEGORY_FAIL_HAVESONNODE);
        }
        ItemCategory theSameParentItemCategory = new ItemCategory();
        theSameParentItemCategory.setParentId(itemCategory.getParentId());
        List<ItemCategory> selectParent = itemCategoryMapper.select(theSameParentItemCategory);
        //判断是否有其他的子节点 注意此处：由于判断在前，所有这个集合查询出来size必定为1
        if (selectParent.size() == 1){
            //如果无其他的子节点，则将isparent属性值为false
            ItemCategory itemCategoryParent = new ItemCategory();
            itemCategoryParent.setId(itemCategory.getParentId());
            itemCategoryParent.setIsParent(false);
            itemCategoryMapper.updateByPrimaryKeySelective(itemCategoryParent);
        }
        //删除的逻辑
        int flag = itemCategoryMapper.delete(itemCategory);
        if (1 != flag){
            throw new YLException(ExceptionEnum.DELETE_CATEGORY_FAIL);
        }

        return ResponseEntity.ok("删除成功");

    }


    public ResponseEntity editItemCategory(Long id, String name) {
        ItemCategory itemCategory = new ItemCategory();
        itemCategory.setId(id);
        itemCategory.setName(name);
        int flag = itemCategoryMapper.updateByPrimaryKeySelective(itemCategory);
        if (1!= flag){
            throw new YLException(ExceptionEnum.EDIT_CATEGORY_FAIL);
        }
        return ResponseEntity.ok("ok");
    }


}
